<?php
namespace openapiend\controllers;

use Yii;
use yii\web\Controller;
use common\models\Tools;
use app\components\FileUpload;
/**
 * Site controller
 */
class UploadController extends Controller
{
    public $enableCsrfValidation=false;
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            
        ];
    }
    
    /**
    * [将Base64图片转换为本地图片并保存]
    * @param [Base64] $base64_image_content [要保存的Base64]
    * @param [目录] $path [要保存的路径]
    */
    function base64_image_content($base64_image_content,$path){
       
        //匹配出图片的格式
        $imgroot = rand(1, 999);
        $imgname_tag = rand(10000, 99999);
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
            $type = $result[2];
            $new_file = $path."/".date('Y-m-d',time())."/".$imgroot."/";
            
            \app\components\FileUpload::mkDirIfNotExist($new_file);
            
            $new_file = $new_file.time().$imgname_tag.".".$type;
            
            $base64_string= explode(',', $base64_image_content); //截取data:image/png;base64, 这个逗号后的字符
            $data= base64_decode($base64_string[1]);
            
            
            if (file_put_contents($new_file, $data)){
                return $new_file;
            }else{
                return "";
            }
        }else{
            return "";
        }
    }
    
    /***
     * 商品图片上传
     */
    public function actionGoodsimgupload(){
        
        $imgdata = Tools::GetSafeParam('imgdata', "", 1);
        
        
//        Tools::outputjson($imgdata);
//        $imgdata = stripslashes($imgdata);
        
        $path = Yii::$app->params['goodsimg'];
        
        $result = [];
        $result['success'] = 1;
        
        if($imgdata==""){
            $result['success'] = 0;
            $result['msg'] = '错误的数据！';
            Tools::outputjson($result);
        }
        
//        $url = [];
        $data_url = $this->base64_image_content($imgdata, $path);
        $result['msg'] = '上传成功！';
        $result['data'] = $data_url;
        \common\models\Tools::outputjson($result);
        
        
//        $file = Tools::GetSafeParam('file', "", 1);
//        
//        $path = Yii::$app->params['goodsimg'];
//        $result = $this->base64_image_content($file, $path);
//        $result = substr($result, 1, strlen($result));
//        echo $result;exit;
        
        
//        $imgroot = rand(1, 999);
//        $date = date("Y-m-d");
//        $type = $_FILES["file"]["type"];
//        $size = $_FILES["file"]["size"];
//        
//        if (!is_null($_FILES["file"])){
//            $tmpName = $_FILES["file"]["tmp_name"];
//            $fileName = $_FILES["file"]["name"];
//            $extName = substr($fileName, strlen($fileName) - 3);
//            $path = Yii::$app->params['goodsimg'] .DIRECTORY_SEPARATOR . $date. DIRECTORY_SEPARATOR . $imgroot;
//            $path = $path . DIRECTORY_SEPARATOR;
//
//            //目录不存在的话，创建一个
//            \app\components\FileUpload::mkDirIfNotExist($path);
//
//            //要保存到服务器上的文件名
//            $result = $path . md5_file($tmpName) . "." . $extName;
//
//            //上传文件，确保文件唯一性
//            if (!file_exists($result)) {
//                move_uploaded_file($tmpName, $result);
//            }
//            //去掉前面的".",保存到数据库中的信息
//            $result = substr($result, 1, strlen($result));
////            $data['success'] = 1;
////            $data['url'] = $result;
//            echo $result;exit;
////            \common\models\Tools::outputjson($data);
//        }
        
        
//        $output_result = [];
//        $goodsimg = $type = $_FILES["goodsimg"];
//        if(empty($goodsimg)){
//            $data['success'] = 0;
//            $data['msg'] = "数据错误！";
//            Tools::outputjson($data);
//        }
//        $imgroot = rand(1, 999);
//        $date = date("Y-m-d");
//        foreach($goodsimg['size'] as $size_key=>$size_val){
//            if($size_val > 10*1024*1024){//最大上传10M的图片
//                $data['success'] = 0;
//                $data['msg'] = "单个文件不能超过10M，请先压缩一下";
//                Tools::outputjson($data);
//            }
//            
//            if ($goodsimg['type'][$size_key] != "image/gif" && $goodsimg['type'][$size_key] != "image/jpeg" && $goodsimg['type'][$size_key] != "image/pjpeg"){
//                $data['success'] = 0;
//                $data['msg'] = "格式不对，请上传正常格式的图片";
//                Tools::outputjson($data);
//            }
//            $tmpName = $goodsimg['tmp_name'][$size_key];
//            $fileName = $goodsimg['name'][$size_key];
//            $extName = substr($fileName, strlen($fileName) - 3);
//            
//            $path = Yii::$app->params['goodsimg'] .DIRECTORY_SEPARATOR . $date. DIRECTORY_SEPARATOR . $imgroot;
//            $path = $path . DIRECTORY_SEPARATOR;
//            //目录不存在的话，创建一个
//            \app\components\FileUpload::mkDirIfNotExist($path);
//            //要保存到服务器上的文件名
//            $result = $path . md5_file($tmpName) . "." . $extName;
//            //上传文件，确保文件唯一性
//            if (!file_exists($result)) {
//                move_uploaded_file($tmpName, $result);
//            }
//            //去掉前面的".",保存到数据库中的信息
//            $result = substr($result, 1, strlen($result));
//            array_push($output_result,$result);
//        }
//        
//        $data['success'] = 1;
//        $data['urls'] = $output_result;
//        Tools::outputjson($data);
    }
    
    public function actionImg(){
        $type = $_FILES["file"]["type"];
        $size = $_FILES["file"]["size"];
        if($size > 10*1024*1024){//最大上传10M的图片
            $data['success'] = 0;
            $data['msg'] = "文件过大，请先压缩一下";
            Tools::outputjson($data);
        }
        if ($type != "image/gif" && $type != "image/jpeg" && $type != "image/pjpeg"){
            $data['success'] = 0;
            $data['msg'] = "格式不对，请上传正常格式的图片";
            Tools::outputjson($data);
        }
        
        if (!is_null($_FILES["file"])){
            $tmpName = $_FILES["file"]["tmp_name"];
            $fileName = $_FILES["file"]["name"];
            $extName = substr($fileName, strlen($fileName) - 3);
            $imgroot = rand(1, 9999);
            $path = Yii::$app->params['userauth'] . DIRECTORY_SEPARATOR . date('Y-m-d',time()).'/'.$imgroot;
            $path = $path . DIRECTORY_SEPARATOR;

            //目录不存在的话，创建一个
            FileUpload::mkDirIfNotExist($path);

            //要保存到服务器上的文件名
            $result = $path . md5_file($tmpName) . "." . $extName;

            //上传文件，确保文件唯一性
            if (!file_exists($result)) {
                move_uploaded_file($tmpName, $result);
            }
            //去掉前面的".",保存到数据库中的信息
            $result = substr($result, 1, strlen($result));
            $data['success'] = 1;
            $data['url'] = $result;
            Tools::outputjson($data);
        }
    }
    
}
